<?php 
	/**
	 * 每天删除30天以前的数据
	 */

	include_once dirname(dirname(__FILE__))."/include/common.inc.php";
	include_once dirname(dirname(__FILE__))."/baihe/function.baihe.php";

	//抓取日期，默认为昨天
	$ymd =  date("Y-m-d", time()-60*60*24);
	if ($argc > 1) {
		$ymd = date("Y-m-d", strtotime($argv[1]));
	}
	
	//$file = 'user'.$ymd.'.txt';
	$page_dir = ROOT_PATH."/baihe/pages";
	$page_dir = $page_dir."/".$ymd."/";
	$tblName = getTblName($ymd);
	$sql = "select  source_user_id,age,gender,has_photo from $tblName where fetch_date='$ymd' and `from`='baihe'";
	$user = $conn->query($sql);
	
	/*
	$filename = '82255432.html';
	$page = file_get_contents($filename);
	$page = str_replace(array(" ","\t","\n","\r","&nbsp;"), "", $page);
	file_put_contents('aa.txt',$page);
	preg_match('#<prestyle="margin-top:20px;border:0ptnone;overflow:visible;width:98%;">(.*?)</pre>#i', $page,$match);
	isset($match[1]) ? $profile = $match[1] : $profile = '未知';
	echo md5(iconv('gbk', 'utf-8', $profile))."\n";
	echo md5('等待，只为与你相遇，从相识、相爱、到遥远的未来。夕阳下，携手一起慢慢变老。');
	exit;
	$randuser = array();
	*/
	$is3g = $app = $mainsite = $total = 0;
	$md5 = md5('等待，只为与你相遇，从相识、相爱、到遥远的未来。夕阳下，携手一起慢慢变老。');
	while ($row = $conn->fetch_assoc($user)) {
		//echo $row['source_user_id']."\n";
		$filename = $page_dir.$row['source_user_id'].'.html';
		$page = file_get_contents($filename);
		$page = str_replace(array(" ","\t","\n","\r","&nbsp;"), "", $page);
		preg_match('#<prestyle="margin-top:20px;border:0ptnone;overflow:visible;width:98%;">(.*?)</pre>#i', $page,$match);
		isset($match[1]) ? $profile = $match[1] : $profile = '未知';
		if (strpos($profile, '3G') !== false) {
			$is3g++;
		}elseif (md5(iconv('gbk', 'utf-8', $profile)) == $md5) {
			$app++;
		}else{
			$mainsite++;
		}
		//echo md5($profile)."\n";
		//echo md5('等待，只为与你相遇，从相识、相爱、到遥远的未来。夕阳下，携手一起慢慢变老。');
		$total++;
		echo 'is3g'.$is3g.'app'.$app.'mainsite'.$mainsite."\n";
		
	}
$sql = "insert into baiheuser set date='$ymd' , is3g=$is3g, app=$app, mainsite=$mainsite,total=$total,site='baihe'";
echo $sql;
if ($conn->query($sql)) {
	$value = 'baihe';
	$subject = '竞争对手'.$value.'用户分析';
	$content = '竞争对手'.$value.'用户分析：3G用户：'.$is3g.'app用户:'.$app.'网站用户：'.$mainsite;
	sendMailEx($subject, $content);
}else{
	echo 'insert error';
}



?>